package davidlauzon.activerecord.connection;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import davidlauzon.activerecord.visitor.MysqlSerializer;

public class MysqlAdapter extends ConnectionAdapter
{
    static public final String DRIVER       = "mysql";
    static public final int    DEFAULT_PORT = 3306;
    
    public MysqlAdapter(String dbName) throws ClassNotFoundException, SQLException {
        super(dbName);
        setDriver(DRIVER);
        setDbPort(DEFAULT_PORT);
        setSerializer( new MysqlSerializer() );
        Class.forName("com.mysql.jdbc.Driver");
        connect();
    }

    @Override
    public long getLastInsertId() throws SQLException
    {
        long lastInsertId = -1;
        
        Statement statement = createStatement();
        ResultSet set       = statement.executeQuery("SELECT last_insert_id()");
        
        if ( set.next() ) {
            lastInsertId = set.getLong(1);
            
            set.close();
            statement.close();
        }
        
        return lastInsertId;
    }
}
